#define _CRT_SECURE_NO_WARNINGS	

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* removeZeroSumSublists(struct ListNode* head)
{
    ListNode* plist = (ListNode*)malloc(sizeof(ListNode));
    plist->next = head;
    ListNode* start = plist;
    while (start)
    {
        int sum = 0;
        ListNode* pcur = start->next;
        while (pcur)
        {
            sum += pcur->val;
            if (sum == 0)
            {
                ListNode* del = start->next;
                start->next = pcur->next;
                while (del != pcur)
                {
                    ListNode* tmp = del;
                    del = del->next;
                    free(tmp);
                }
            }
            pcur = pcur->next;
        }
        start = start->next;
    }
    head = plist->next;
    free(plist);
    plist = NULL;
    return head;
}